0

Twitter sentiment analysis.ipynb

No Headings

The table of contents shows headings in notebooks and supported files.

Skip to Main
Jupyter

Twitter sentiment analysis

Last Checkpoint: 10 seconds ago
  • File
  • Edit
  • View
  • Run
  • Kernel
  • Settings
  • Help
JupyterLab
Python [conda env:base]
Kernel status: Idle
image/svg+xml
    [1]:
    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import LabelEncoder
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.naive_bayes import MultinomialNB
    from sklearn.ensemble import GradientBoostingClassifier
    from sklearn.metrics import accuracy_score, classification_report
    import spacy
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.pipeline import Pipeline
    import seaborn as sns
    import matplotlib.pyplot as plt
    [3]:
    nlp=spacy.load("en_core_web_sm")
    [5]:
    df=pd.read_csv("C:/Users/rish2/Downloads/archive/twitter_training.csv")
    df
    [5]:
    2401 Borderlands Positive im getting on borderlands and i will murder you all ,
    0 2401 Borderlands Positive I am coming to the borders and I will kill you...
    1 2401 Borderlands Positive im getting on borderlands and i will kill you ...
    2 2401 Borderlands Positive im coming on borderlands and i will murder you...
    3 2401 Borderlands Positive im getting on borderlands 2 and i will murder ...
    4 2401 Borderlands Positive im getting into borderlands and i can murder y...
    ... ... ... ... ...
    74676 9200 Nvidia Positive Just realized that the Windows partition of my...
    74677 9200 Nvidia Positive Just realized that my Mac window partition is ...
    74678 9200 Nvidia Positive Just realized the windows partition of my Mac ...
    74679 9200 Nvidia Positive Just realized between the windows partition of...
    74680 9200 Nvidia Positive Just like the windows partition of my Mac is l...

    74681 rows × 4 columns

    [7]:
    columns =["ID","Entity","Label","Text"]
    df=pd.read_csv("C:/Users/rish2/Downloads/archive/twitter_training.csv",names=columns)
    df
    [7]:
    ID Entity Label Text
    0 2401 Borderlands Positive im getting on borderlands and i will murder yo...
    1 2401 Borderlands Positive I am coming to the borders and I will kill you...
    2 2401 Borderlands Positive im getting on borderlands and i will kill you ...
    3 2401 Borderlands Positive im coming on borderlands and i will murder you...
    4 2401 Borderlands Positive im getting on borderlands 2 and i will murder ...
    ... ... ... ... ...
    74677 9200 Nvidia Positive Just realized that the Windows partition of my...
    74678 9200 Nvidia Positive Just realized that my Mac window partition is ...
    74679 9200 Nvidia Positive Just realized the windows partition of my Mac ...
    74680 9200 Nvidia Positive Just realized between the windows partition of...
    74681 9200 Nvidia Positive Just like the windows partition of my Mac is l...

    74682 rows × 4 columns

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 74682 entries, 0 to 74681
    Data columns (total 4 columns):
     #   Column  Non-Null Count  Dtype 
    ---  ------  --------------  ----- 
     0   ID      74682 non-null  int64 
     1   Entity  74682 non-null  object
     2   Label   74682 non-null  object
     3   Text    73996 non-null  object
    dtypes: int64(1), object(3)
    memory usage: 2.3+ MB
    
    [15]:
    Entity
    LeagueOfLegends                      2377
    MaddenNFL                            2377
    CallOfDuty                           2376
    Verizon                              2365
    TomClancysRainbowSix                 2364
    Facebook                             2362
    Microsoft                            2361
    Dota2                                2359
    WorldOfCraft                         2357
    ApexLegends                          2353
    NBA2K                                2343
    CallOfDutyBlackopsColdWar            2343
    johnson&johnson                      2324
    FIFA                                 2324
    TomClancysGhostRecon                 2321
    Overwatch                            2316
    Battlefield                          2316
    GrandTheftAuto(GTA)                  2293
    HomeDepot                            2292
    PlayStation5(PS5)                    2291
    Hearthstone                          2286
    CS-GO                                2284
    Xbox(Xseries)                        2283
    Borderlands                          2280
    Amazon                               2276
    Google                               2274
    Nvidia                               2271
    Cyberpunk2077                        2262
    RedDeadRedemption(RDR)               2249
    Fortnite                             2249
    AssassinsCreed                       2234
    PlayerUnknownsBattlegrounds(PUBG)    2234
    Name: count, dtype: int64
    [19]:
    Label
    Negative      22358
    Positive      20655
    Neutral       18108
    Irrelevant    12875
    Name: count, dtype: int64
    1: im getting on borderlands and i will murder you all , -> Positive
    2: I am coming to the borders and I will kill you all, -> Positive
    3: im getting on borderlands and i will kill you all, -> Positive
    4: im coming on borderlands and i will murder you all, -> Positive
    5: im getting on borderlands 2 and i will murder you me all, -> Positive
    6: im getting into borderlands and i can murder you all, -> Positive
    7: So I spent a few hours making something for fun. . . If you don't know I am a HUGE @Borderlands fan and Maya is one of my favorite characters. So I decided to make myself a wallpaper for my PC. . Here is the original image versus the creation I made :) Enjoy! pic.twitter.com/mLsI5wf9Jg -> Positive
    8: So I spent a couple of hours doing something for fun... If you don't know that I'm a huge @ Borderlands fan and Maya is one of my favorite characters, I decided to make a wallpaper for my PC.. Here's the original picture compared to the creation I made:) Have fun! pic.twitter.com / mLsI5wf9Jg -> Positive
    9: So I spent a few hours doing something for fun... If you don't know I'm a HUGE @ Borderlands fan and Maya is one of my favorite characters. -> Positive
    10: So I spent a few hours making something for fun. . . If you don't know I am a HUGE RhandlerR fan and Maya is one of my favorite characters. So I decided to make myself a wallpaper for my PC. . Here is the original image versus the creation I made :) Enjoy! pic.twitter.com/mLsI5wf9Jg -> Positive
    
    [31]:
    ID Entity Label Text Preprocessed Text
    0 2401 Borderlands Positive im getting on borderlands and i will murder yo... m get borderland murder
    1 2401 Borderlands Positive I am coming to the borders and I will kill you... come border kill
    2 2401 Borderlands Positive im getting on borderlands and i will kill you ... m get borderland kill
    3 2401 Borderlands Positive im coming on borderlands and i will murder you... m come borderland murder
    4 2401 Borderlands Positive im getting on borderlands 2 and i will murder ... m get borderland 2 murder
    ... ... ... ... ... ...
    74677 9200 Nvidia Positive Just realized that the Windows partition of my... realize Windows partition Mac like 6 year Nvid...
    74678 9200 Nvidia Positive Just realized that my Mac window partition is ... realize Mac window partition 6 year Nvidia dri...
    74679 9200 Nvidia Positive Just realized the windows partition of my Mac ... realize window partition Mac 6 year Nvidia dri...
    74680 9200 Nvidia Positive Just realized between the windows partition of... realize window partition Mac like 6 year Nvidi...
    74681 9200 Nvidia Positive Just like the windows partition of my Mac is l... like window partition Mac like 6 year driver i...

    73996 rows × 5 columns

    [35]:
    ID Entity Label Text Preprocessed Text
    0 2401 Borderlands 3 im getting on borderlands and i will murder yo... m get borderland murder
    1 2401 Borderlands 3 I am coming to the borders and I will kill you... come border kill
    2 2401 Borderlands 3 im getting on borderlands and i will kill you ... m get borderland kill
    3 2401 Borderlands 3 im coming on borderlands and i will murder you... m come borderland murder
    4 2401 Borderlands 3 im getting on borderlands 2 and i will murder ... m get borderland 2 murder
    ... ... ... ... ... ...
    74677 9200 Nvidia 3 Just realized that the Windows partition of my... realize Windows partition Mac like 6 year Nvid...
    74678 9200 Nvidia 3 Just realized that my Mac window partition is ... realize Mac window partition 6 year Nvidia dri...
    74679 9200 Nvidia 3 Just realized the windows partition of my Mac ... realize window partition Mac 6 year Nvidia dri...
    74680 9200 Nvidia 3 Just realized between the windows partition of... realize window partition Mac like 6 year Nvidi...
    74681 9200 Nvidia 3 Just like the windows partition of my Mac is l... like window partition Mac like 6 year driver i...

    73996 rows × 5 columns

    Shape of X_train:  (59196,)
    Shape of X_test:  (14800,)
    
    [43]:
    Pipeline(steps=[('vectorizer_tri_grams', TfidfVectorizer()),
                    ('naive_bayes', MultinomialNB())])
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    Pipeline(steps=[('vectorizer_tri_grams', TfidfVectorizer()),
                    ('naive_bayes', MultinomialNB())])
    TfidfVectorizer()
    MultinomialNB()
    0.7322972972972973
    
                  precision    recall  f1-score   support
    
               0       0.95      0.46      0.62      2575
               1       0.65      0.90      0.76      4472
               2       0.84      0.64      0.72      3622
               3       0.71      0.81      0.76      4131
    
        accuracy                           0.73     14800
       macro avg       0.79      0.70      0.71     14800
    weighted avg       0.77      0.73      0.72     14800
    
    
    [53]:
    Pipeline(steps=[('vectorizer_tri_grams', TfidfVectorizer()),
                    ('naive_bayes', RandomForestClassifier())])
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    Pipeline(steps=[('vectorizer_tri_grams', TfidfVectorizer()),
                    ('naive_bayes', RandomForestClassifier())])
    TfidfVectorizer()
    RandomForestClassifier()
    0.9118918918918919
    
                  precision    recall  f1-score   support
    
               0       0.97      0.86      0.91      2575
               1       0.92      0.93      0.93      4472
               2       0.94      0.89      0.91      3622
               3       0.85      0.94      0.90      4131
    
        accuracy                           0.91     14800
       macro avg       0.92      0.91      0.91     14800
    weighted avg       0.91      0.91      0.91     14800
    
    
    [63]:
    ID Entity Label Text
    0 2401 Borderlands Positive im getting on borderlands and i will murder yo...
    1 2401 Borderlands Positive I am coming to the borders and I will kill you...
    2 2401 Borderlands Positive im getting on borderlands and i will kill you ...
    3 2401 Borderlands Positive im coming on borderlands and i will murder you...
    4 2401 Borderlands Positive im getting on borderlands 2 and i will murder ...

    -

    Variables

    Callstack

      Breakpoints

      Source

      9
      1

      Kernel Sources

      Common Tools
      No metadata.
      Advanced Tools
      No metadata.
      Anaconda Assistant
      AI-powered coding, insights and debugging in your notebooks.
      To enable the following extensions, create an account or sign in.
      • Anaconda Assistant
        4.1.0
      • Coming soon!
      • Data Catalogs
      • Panel Deployments
      • Sharing
      Already have an account? Sign In
      For more information, read our Anaconda Assistant documentation.
      Alt+[
      Alt+]
      Alt+End
      • Assistant
      • Open Anaconda Assistant
        Ctrl+Shift+A
      • Console
      • Change Kernel…
      • Clear Console Cells
      • Close and Shut Down…
      • Insert Line Break
      • Interrupt Kernel
      • New Console
      • Restart Kernel…
      • Run Cell (forced)
      • Run Cell (unforced)
      • Show All Kernel Activity
      • Debugger
      • Breakpoints on exception
      • Evaluate Code
        Evaluate Code
      • Next
        Next
        F10
      • Pause
        Pause
        F9
      • Step In
        Step In
        F11
      • Step Out
        Step Out
        Shift+F11
      • Terminate
        Terminate
        Shift+F9
      • Display Languages
      • English
        English
      • File Operations
      • Autosave Documents
      • Download
        Download the file to your computer
      • Reload Notebook from Disk
        Reload contents from disk
      • Revert Notebook to Checkpoint…
        Revert contents to previous checkpoint
      • Save Notebook
        Save and create checkpoint
        Ctrl+S
      • Save Notebook As…
        Save with new path
        Ctrl+Shift+S
      • Trust HTML File
        Whether the HTML file is trusted. Trusting the file allows scripts to run in it, which may result in security risks. Only enable for files you trust.
      • Help
      • About Jupyter Notebook
      • Jupyter Reference
      • JupyterLab FAQ
      • JupyterLab Reference
      • Launch Jupyter Notebook File Browser
      • Markdown Reference
      • Show Keyboard Shortcuts…
        Show relevant keyboard shortcuts for the current active widget
        Ctrl+Shift+H
      • Image Viewer
      • Flip image horizontally
        H
      • Flip image vertically
        V
      • Invert Colors
        I
      • Reset Image
        0
      • Rotate Clockwise
        ]
      • Rotate Counterclockwise
        [
      • Zoom In
        =
      • Zoom Out
        -
      • Kernel Operations
      • Shut Down All Kernels…
      • Main Area
      • Close All Other Tabs
      • Close Tab
        Alt+W
      • Close Tabs to Right
      • End Search
        Esc
      • Find Next
        Ctrl+G
      • Find Previous
        Ctrl+Shift+G
      • Find…
        Ctrl+F
      • Log Out
        Log out of Jupyter Notebook
      • Search in Selection
        Alt+L
      • Shut Down
        Shut down Jupyter Notebook
      • Mode
      • Toggle Zen Mode
      • Notebook Cell Operations
      • Change to Code Cell Type
        Y
      • Change to Heading 1
        1
      • Change to Heading 2
        2
      • Change to Heading 3
        3
      • Change to Heading 4
        4
      • Change to Heading 5
        5
      • Change to Heading 6
        6
      • Change to Markdown Cell Type
        M
      • Change to Raw Cell Type
        R
      • Clear Cell Output
        Clear outputs for the selected cells
      • Collapse All Code
      • Collapse All Outputs
      • Collapse Selected Code
      • Collapse Selected Outputs
      • Copy Cell
        Copy this cell
        C
      • Cut Cell
        Cut this cell
        X
      • Delete Cell
        Delete this cell
        D, D
      • Disable Scrolling for Outputs
      • Enable Scrolling for Outputs
      • Expand All Code
      • Expand All Outputs
      • Expand Selected Code
      • Expand Selected Outputs
      • Extend Selection Above
        Shift+K
      • Extend Selection Below
        Shift+J
      • Extend Selection to Bottom
        Shift+End
      • Extend Selection to Top
        Shift+Home
      • Insert Cell Above
        Insert a cell above
        A
      • Insert Cell Below
        Insert a cell below
        B
      • Insert Heading Above Current Heading
        Shift+A
      • Insert Heading Below Current Heading
        Shift+B
      • Merge Cell Above
        Ctrl+Backspace
      • Merge Cell Below
        Ctrl+Shift+M
      • Merge Selected Cells
        Shift+M
      • Move Cell Down
        Move this cell down
        Ctrl+Shift+Down
      • Move Cell Up
        Move this cell up
        Ctrl+Shift+Up
      • Paste Cell Above
        Paste this cell from the clipboard
      • Paste Cell and Replace
      • Paste Cell Below
        Paste this cell from the clipboard
        V
      • Redo Cell Operation
        Shift+Z
      • Render Side-by-Side
        Shift+R
      • Run Selected Cell
        Run this cell and advance
        Shift+Enter
      • Run Selected Cell and Do not Advance
        Ctrl+Enter
      • Run Selected Cell and Insert Below
        Alt+Enter
      • Run Selected Text or Current Line in Console
      • Select Cell Above
        K
      • Select Cell Below
        J
      • Select Heading Above or Collapse Heading
        Left
      • Select Heading Below or Expand Heading
        Right
      • Set side-by-side ratio
      • Split Cell
        Ctrl+Shift+-
      • Undo Cell Operation
        Z
      • Notebook Operations
      • Access Next Kernel History Entry
        Alt+Down
      • Access Previous Kernel History Entry
        Alt+Up
      • Change Kernel…
      • Clear Outputs of All Cells
        Clear all outputs of all cells
      • Close and Shut Down Notebook…
      • Collapse All Headings
        Ctrl+Shift+Left
      • Deselect All Cells
      • Edit Notebook Metadata
      • Enter Command Mode
        Ctrl+M
      • Enter Edit Mode
        Enter
      • Expand All Headings
        Ctrl+Shift+Right
      • Interrupt Kernel
        Interrupt the kernel
      • New Console for Notebook
      • New Notebook
        Create a new notebook
      • Open with Panel in New Browser Tab
      • Preview Notebook with Panel
      • Reconnect to Kernel
      • Render All Markdown Cells
      • Restart Kernel and Clear Outputs of All Cells…
        Restart the kernel and clear all outputs of all cells
      • Restart Kernel and Debug…
        Restart Kernel and Debug…
      • Restart Kernel and Run All Cells…
        Restart the kernel and run all cells
      • Restart Kernel and Run up to Selected Cell…
      • Restart Kernel…
        Restart the kernel
      • Run All Above Selected Cell
      • Run All Cells
        Run all cells
      • Run Selected Cell and All Below
      • Save and Export Notebook: Asciidoc
      • Save and Export Notebook: Executable Script
      • Save and Export Notebook: HTML
      • Save and Export Notebook: LaTeX
      • Save and Export Notebook: Markdown
      • Save and Export Notebook: PDF
      • Save and Export Notebook: Qtpdf
      • Save and Export Notebook: Qtpng
      • Save and Export Notebook: ReStructured Text
      • Save and Export Notebook: Reveal.js Slides
      • Save and Export Notebook: Webpdf
      • Select All Cells
        Ctrl+A
      • Show Line Numbers
      • Toggle Collapse Notebook Heading
      • Trust Notebook
      • Other
      • Open in JupyterLab
        JupyterLab
      • Plugin Manager
      • Advanced Plugin Manager
      • Terminal
      • Decrease Terminal Font Size
      • Increase Terminal Font Size
      • New Terminal
        Start a new terminal session
      • Refresh Terminal
        Refresh the current terminal session
      • Use Terminal Theme: Dark
        Set the terminal theme
      • Use Terminal Theme: Inherit
        Set the terminal theme
      • Use Terminal Theme: Light
        Set the terminal theme
      • Text Editor
      • Decrease Font Size
      • Increase Font Size
      • New Markdown File
        Create a new markdown file
      • New Python File
        Create a new Python file
      • New Text File
        Create a new text file
      • Spaces: 1
      • Spaces: 2
      • Spaces: 4
      • Spaces: 4
      • Spaces: 8
      • Theme
      • Decrease Code Font Size
      • Decrease Content Font Size
      • Decrease UI Font Size
      • Increase Code Font Size
      • Increase Content Font Size
      • Increase UI Font Size
      • Set Preferred Dark Theme: JupyterLab Dark
      • Set Preferred Dark Theme: JupyterLab Dark High Contrast
      • Set Preferred Dark Theme: JupyterLab Light
      • Set Preferred Light Theme: JupyterLab Dark
      • Set Preferred Light Theme: JupyterLab Dark High Contrast
      • Set Preferred Light Theme: JupyterLab Light
      • Synchronize Styling Theme with System Settings
      • Theme Scrollbars
      • Use Theme: JupyterLab Dark
      • Use Theme: JupyterLab Dark High Contrast
      • Use Theme: JupyterLab Light
      • View
      • File Browser
      • Open JupyterLab
      • Show Anaconda Assistant
        Show Show Anaconda Assistant in the right sidebar
      • Show Debugger
        Show Show Debugger in the right sidebar
      • Show Header
      • Show Notebook Tools
        Show Show Notebook Tools in the right sidebar
      • Show Table of Contents
        Show Show Table of Contents in the left sidebar